package com.lovo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/14
 */
//扫描basePackages下所有接口, 等同于mybaties映射包,
//让这个类变成配置类, 来完全消除xml
@Configuration
//扫描basePackages下所有带Component注解的东西, 注册进spring容器
@ComponentScan(basePackages = "com.lovo")
//加载指定配置文件
@PropertySource("classpath:db.properties")
//代理类有接口要写这个proxyTargetClass = true
@EnableAspectJAutoProxy(proxyTargetClass = true)
//打开事务
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.lovo.dao")
public class AppConfig {
    @Resource
    private DBProperties dbProperties;

    @Bean
    //数据源
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dbProperties.getUrl());
        dataSource.setDriverClassName(dbProperties.getDriver());
        dataSource.setUsername(dbProperties.getUser());
        dataSource.setPassword(dbProperties.getPwd());
        return dataSource;
    }

    @Bean
    public HibernateJpaVendorAdapter hibernateJpaVendorAdapter(){
        return new HibernateJpaVendorAdapter();
    }
    //创建事务管理器对象
    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager dataSourceTransactionManager = new JpaTransactionManager();
        dataSourceTransactionManager.setEntityManagerFactory(entityManagerFactory);
        return dataSourceTransactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter hibernateJpaVendorAdapter){
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);

        entityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter);

        entityManagerFactoryBean.setPackagesToScan(dbProperties.getPackageScan());
        Properties prop = new Properties();
        prop.setProperty("hibernate.dialect",dbProperties.getDialect());
        prop.setProperty("hibernate.show_sql",dbProperties.isShowSql() + "");
        prop.setProperty("hibernate.format_sql",dbProperties.isFormatSql() + "");
        entityManagerFactoryBean.setJpaProperties(prop);
        return entityManagerFactoryBean;
    }
}
